﻿using EU.Core.Common;
using EU.Core.Common.LogHelper;
using EU.Core.Hubs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;

namespace EU.Core.Extensions.Middlewares;

/// <summary>
/// 中间件
/// SignalR发送数据
/// </summary>
public class SignalRSendMiddleware
{
    /// <summary>
    /// 
    /// </summary>
    private readonly RequestDelegate _next;
    private readonly IHubContext<ChatHub> _hubContext;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="next"></param>
    /// <param name="hubContext"></param>
    public SignalRSendMiddleware(RequestDelegate next, IHubContext<ChatHub> hubContext)
    {
        _next = next;
        _hubContext = hubContext;
    }



    public async Task InvokeAsync(HttpContext context)
    {
        if (AppSettings.app("Middleware", "SignalR", "Enabled").ObjToBool())
        {
            //TODO 主动发送错误消息
            await _hubContext.Clients.All.SendAsync("ReceiveUpdate", LogLock.GetLogData()); 
        }
        await _next(context);
    }

}

